package com.txazo.service.impl;

import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;

import com.txazo.domain.User;
import com.txazo.eao.JpaEao;
import com.txazo.service.UserService;

/**
 * 用户模块Service实现
 * 
 * @author txazo
 * @since 1.0
 * 
 */
@Stateless(name = "userService")
@TransactionManagement(TransactionManagementType.CONTAINER)
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class UserServiceImpl implements UserService {

	@EJB(name = "jpaEao")
	private JpaEao jpaEao;

	@Override
	public User login(User user) {
		List<User> list = jpaEao.findAll(User.class,
				"from User where username = ? and password = ?", new Object[] {
						user.getUsername(), user.getPassword() });
		if (list.size() == 1) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public User register(User user) {
		List<User> list = jpaEao.findAll(User.class,
				"from User where username = ?", user.getUsername());
		if (list.size() == 0) {
			jpaEao.save(user);
			return user;
		}
		return null;
	}

}
